package cn.zhf.shop.utils;

import java.text.SimpleDateFormat;
import java.util.*;

public class GetEveryDate {

    //获得所在周的每一天
    public static List<String> dateToWeek(Date mdate){

        int b=mdate.getDay();
        if (b==0)
        {
            b=7;
        }
        Date fdate ;
        List <String> list = new ArrayList();
        Long fTime=mdate.getTime()-(b-1)*24*3600000;
        for(int a=0;a<7;a++)
        {
            fdate= new Date();
            fdate.setTime(fTime+(a*24*3600000));
            String dt = new SimpleDateFormat("yyyy-MM-dd").format(fdate);
            list.add(a, dt);
        }

        return list;
    }

    //获得所在月的每一天
    public static List<String> getDayByMonth(Date mdate){

        //获取date对应的Calendar对象

        Calendar ca = Calendar.getInstance();

        ca.setTime(mdate);

        //可以从ca中获取各种该日期的属性值：

        int monthParam = ca.get(Calendar.MONTH);//第几个月

        int yearParam = ca.get(Calendar.YEAR);//年份数值

        List list = new ArrayList();
        Calendar aCalendar = Calendar.getInstance(Locale.CHINA);
        aCalendar.set(yearParam,monthParam,1);
        int year = aCalendar.get(Calendar.YEAR);//年份
        int month = aCalendar.get(Calendar.MONTH) + 1;//月份
        int day = aCalendar.getActualMaximum(Calendar.DATE);
        for (int i = 1; i <= day; i++) {
            String aDate=null;
            if(month<10&&i<10){
                aDate = String.valueOf(year)+"-0"+month+"-0"+i;
            }
            if(month<10&&i>=10){
                aDate = String.valueOf(year)+"-0"+month+"-"+i;
            }
            if(month>=10&&i<10){
                aDate = String.valueOf(year)+"-"+month+"-0"+i;
            }
            if(month>=10&&i>=10){
                aDate = String.valueOf(year)+"-"+month+"-"+i;
            }

            list.add(aDate);
        }
        return list;
    }

    //获得所在季度每个月
    public static List<String> getQuarterByMonth(Date mdate){
        //获取date对应的Calendar对象

        Calendar ca = Calendar.getInstance();

        ca.setTime(mdate);

        //可以从ca中获取各种该日期的属性值：

        int monthParam = ca.get(Calendar.MONTH)+1;//第几个月



        int yearParam = ca.get(Calendar.YEAR);//年份数值

        if (monthParam>=1&&monthParam<=3)
        {
            List<String> stringList = new LinkedList<>();
            for (int i=1;i<=3;i++)
            {
                //yyyy-MM
                String month = String.valueOf(yearParam)+"-"+"0"+String.valueOf(i);
                stringList.add(month);
            }
            return stringList;
        }
        else if (monthParam>=4&&monthParam<=6)
        {
            List<String> stringList = new LinkedList<>();
            for (int i=4;i<=6;i++)
            {
                //yyyy-MM
                String month = String.valueOf(yearParam)+"-"+"0"+String.valueOf(i);
                stringList.add(month);
            }
            return stringList;
        }
        else if (monthParam>=7&&monthParam<=9)
        {
            List<String> stringList = new LinkedList<>();
            for (int i=7;i<=9;i++)
            {
                //yyyy-MM
                String month = String.valueOf(yearParam)+"-"+"0"+String.valueOf(i);
                stringList.add(month);
            }
            return stringList;
        }
        else if (monthParam>=10&&monthParam<=12)
        {
            List<String> stringList = new LinkedList<>();
            for (int i=10;i<=12;i++)
            {
                //yyyy-MM
                String month = String.valueOf(yearParam)+"-"+String.valueOf(i);
                stringList.add(month);
            }
            return stringList;
        }
        return null;
    }

    //获得所在年的每个月
    public static List<String> getYearByMonth(Date mdate){
        //获取date对应的Calendar对象

        Calendar ca = Calendar.getInstance();

        ca.setTime(mdate);

        //可以从ca中获取各种该日期的属性值：
        int yearParam = ca.get(Calendar.YEAR);//年份数值

        List<String> stringList = new LinkedList<>();
            for (int i=1;i<=9;i++)
            {
                //yyyy-MM
                String month = String.valueOf(yearParam)+"-"+"0"+String.valueOf(i);
                stringList.add(month);
            }
        for (int i=10;i<=12;i++)
        {
            //yyyy-MM
            String month = String.valueOf(yearParam)+"-"+String.valueOf(i);
            stringList.add(month);
        }

        return stringList;

    }

}
